"use strict";
export function log(...a) {
	console.log(a);
}
export function existKey(obj,key){
	if (obj instanceof Object) {		
		for (let attr in obj) {
			if(key==attr)
			return true;
		}
		return false;
	}
	return false
}
/**
 * 判断对象中是否存在某个值
 * (不判断键)
 * @param {*} obj 
 * @param {*} value 
 * @returns 
 */
export function existValue(obj,value){	
	if (obj instanceof Array) {
		for (let i = 0; i < obj.length; ++i) {
			if(value==obj[i])
			return true;
		}
		return false;
	}
	if (obj instanceof Object) {		
		for (let attr in obj) {
			if(value==obj[attr])
			return true;
		}
		return false;
	}
	//字符串
	if ('string'==typeof obj ) {	
		return (obj.indexOf(value)>-1);
	}
	return false;
}
/**
 * 深度克隆一个对象 
 * @param {Any} obj 
 * @returns 
 */
export function clone(obj) {
	if (null == obj || "object" != typeof obj) return obj;
	if (obj instanceof Date) {
		var copy = new Date();
		copy.setTime(obj.getTime());
		return copy;
	}
	if (obj instanceof Array) {
		var copy = [];
		for (let i = 0; i < obj.length; ++i) {
			copy.push(clone(obj[i]));
		}
		return copy;
	}
	if (obj instanceof Object) {
		var copy = {};
		for (let attr in obj) {
			copy[attr] = clone(obj[attr]);
		}
		return copy;
	}
	return obj;
	//throw new Error("这个对象不支持拷贝.");
}

/**仅复制 字典中 需要的项
 * @param {Object} 数据源
 * @param {Array<String>} 字段数组 需要的项逗号分隔
 * @param {Boolean} 筛排 筛选 true,排除 false
 */
export function cloneMap(数据源, 字段数组, 筛排) {
	if (null == 数据源|| "object" != typeof 数据源) return 数据源;	
	if (数据源 instanceof Object) {
		let copy = {};
		if (字段数组&&字段数组.length>0){
			let isselect=筛排||true;
			for (let attr in 数据源) {	
				let a=existValue(字段数组,attr)				
				if(!isselect^a) copy[attr] = clone(数据源[attr]);
			}		
		} else {			
			for (let attr in 数据源) {
				copy[attr] = clone(数据源[attr]);
			}
		}
		return copy;
	}
	return 数据源;
}

/**
 * 带默认值的克隆
 * 
 * 只有字典源的键 在key_default集合中的字典源的键值才会被复制
 * @param {Map} 目标 
 * @param {Map} 字典源  只有在key 集合中的键值才会被复制
 * @param {Map} key_default {keyn:默认值n} 
 */
export function extendDef(目标, 字典源, key_default) {
	if (字典源) {
		if (字典源 instanceof Object) {
			for (let key in key_default) {
				目标[key] = 字典源[key] || key_default[key]
			}
		}
	}
}

/**
 * 等效于Object.assign({}, ...item); 但是做了扩展
 *  @param {Object} 目标
 *  @param {Object} 数据源 
 *  @param {Array} 字段数组 
 *  @param {Boolean} 筛排 筛选 true,排除 false
 */
export function extend(目标,数据源,字段数组, 筛排) {	
	if (字段数组&&字段数组.length>0){	
		let isselect=筛排||true;
		for (let i=0;i<字段数组.length;i++){
			let key=字段数组[i];
			let a=(key!=null&& key!='' && 数据源.hasOwnProperty(key));			
			if(!isselect^a)	目标[key] = 数据源[key];
		}		
	}else{
		for (let prp in 数据源) {
			目标[prp] = 数据源[prp];
		}
	}	
}
/**

 * 组合map中的值 间隔符默认是换行符

 * @param {Map} obj 
 * @param {*} 分隔符 
 * @param {*} 起始符 
 * @param {*} 结束符 
 * @returns 
 */
export function join(obj, 分隔符, 起始符, 结束符) {
	let str = "";
	let sp = 分隔符 || '\n';
	if (obj instanceof Object) {
		for (var key in obj)
			str += key + ":" + obj[key] + sp;
	}
	if (obj instanceof Array) {
		for (let i = 0; i < obj.length; ++i) {
			str += obj[i] + sp;
		}
	}
	起始符 = 起始符 || '';
	结束符 = 结束符 || ''
	return 起始符 + str.slice(0, str.length - 1) + 结束符;
	///console.log("反射", name);
}

function Assembly(obj) {
	var name = "";
	for (var name1 in obj)
		name += name1 + ":" + obj[name1] + "\n";
	///console.log("反射", name);
}
export function round2(number, fractiondigits) {
	return round(number * Math.pow(10, fractiondigits)) / Math.pow(10, fractiondigits);
}
export var _IS = new(function() {
	var gettype = Object.prototype.toString;
	this.isMap = function(o) {
		return gettype.call(o) == "[object Object]";
	}
	this.isBlob = function(o) {
		return o instanceof Blob;
	}

	this.isArray = function(o) {
		return gettype.call(o) == "[object Array]";
	}
	this.isBuffer = function(o) {
		return gettype.call(o) == "[object ArrayBuffer]";
	}
	this.isFun = function(o) {
		return gettype.call(o) == "[object Function]";
	}
	this.isNULL = function(o) {
		return gettype.call(o) == "[object Null]";
	}
	this.isUndefined = function(o) {
		return gettype.call(o) == "[object Undefined]";
	}
	this.isNum = function(o) {
		return gettype.call(o) == "[object Number]";
	}
	this.isString = function(o) {
		return gettype.call(o) == "[object String]";
	}
	this.isBool = function(o) {
		return gettype.call(o) == "[object Boolean]";
	}
	this.isSet = function(o) {
		return gettype.call(o) == "[object Set]";
	}
	this.isEmptyMap = function(e) {
		for (var t in e)
			return false;
		return true
	}
	this.isEmpty= function(str) {
		if (str == null )
			return true
		else
			return false
	}
	this.isEmptyString = function(str) {
		if (str == null || str == '')
			return true
		else
			return false
	}
	this.isEmptyArray = function(arr) {
		if (this.isArray(arr) || arr.length == 0)
			return true
		else
			return false
	}
	this.isEmptyObject= function (obj) {
		//console.log("isEmptyObject", Object.keys(obj).length)
		if (Object.keys(obj).length === 0) {
			return true;
		} else {
			return false;
		}
	}
})();

export function uuid() {
	var s = [];
	var hexDigits = "0123456789abcdef";
	for (var i = 0; i < 36; i++) {
		s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
	}
	s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
	s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
	s[8] = s[13] = s[18] = s[23] = "-";

	var uuid = s.join("");
	return uuid;
}

function S4() {
	return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}
export function uuid32() {
	return S4() + S4() + S4() + S4() + S4() + S4() + S4() + S4();
}
export function checkcode4() {
	return S4();
}


export function getGuid() {
	var d = new Date().getTime();
	var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
		var r = (d + Math.random() * 16) % 16 | 0;
		d = Math.floor(d / 16);
		return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
	});
	return uuid;
}



